//
//  NEBaseTeamSearchUserCell.swift
//  NETeamUIKit
//
//  Created by Laura on 2024/7/31.
//


import NEChatUIKit
import NECommonKit
import NIMSDK
import UIKit

@objcMembers
open class NEBaseTeamSearchUserCell: UITableViewCell {
  /// 搜索文案(用户匹配高亮)
  public var user: String?
  /// 高亮颜色
  public var rangeTextColor = NEConstant.hexRGB(0xFF8503)

  /// 用户头像视图
  public lazy var headView: NEUserHeaderView = {
    let headView = NEUserHeaderView(frame: .zero)
    headView.titleLabel.textColor = .white
    headView.titleLabel.font = NEConstant.defaultTextFont(14)
    headView.translatesAutoresizingMaskIntoConstraints = false
    headView.layer.cornerRadius = 6
    headView.clipsToBounds = true
    return headView
  }()

  /// 用户昵称
  public lazy var titleLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .normalTeamTitleColor
    label.font = NEConstant.defaultTextFont(16)
    label.textAlignment = .left
    label.accessibilityIdentifier = "id.name"
    return label
  }()

  /// 消息内容
  public lazy var subTitleLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .normalTeamSubTitleColor
    label.font = NEConstant.defaultTextFont(13)
    label.textAlignment = .left
    label.accessibilityIdentifier = "id.message"
    return label
  }()

  /// 分隔线
  public lazy var bottomLine: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
//    view.backgroundColor = UIColor(hexString: "0xDBE0E8")
    return view
  }()

  /// 消息时间
  public lazy var timeLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .normalTeamSubTitleColor
    label.font = NEConstant.defaultTextFont(13)
    label.textAlignment = .right
    label.accessibilityIdentifier = "id.time"
    return label
  }()

  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupSubviews()
  }

  public required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  open func setupSubviews() {
    selectionStyle = .none
    contentView.addSubview(headView)
    contentView.addSubview(titleLabel)
    contentView.addSubview(subTitleLabel)
    contentView.addSubview(bottomLine)
    contentView.addSubview(timeLabel)
  }

  open func configData(message: V2NIMMessage, user: NECoreIM2Kit.NEUserWithFriend?) {
      titleLabel.text = user?.showName()
      subTitleLabel.text = ChatMessageHelper.contentOfMessage(message)
      
      let messageTime = Date(timeIntervalSince1970: message.createTime)
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "yyyy/MM/dd"
      let dateString = dateFormatter.string(from: messageTime)
      timeLabel.text = dateString

      if let imageName = user?.user?.avatar, !imageName.isEmpty {
          headView.setTitle("")
          headView.sd_setImage(with: URL(string: imageName), completed: nil)
      } else {
          headView.setTitle(user?.user?.shortName() ?? "")
          headView.sd_setImage(with: nil, completed: nil)
          headView.backgroundColor = UIColor.colorWithString(string:  message.senderId ?? "")
      }
  }
}
